package com.gang.mars.redisson.lock;

import lombok.SneakyThrows;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 学习参考 周瑜 https://www.bilibili.com/video/BV1D54y1H7Bh?p=4&spm_id_from=pageDriver
 *
 * 加锁 lock ->线程阻塞以下几个方式
 *  wait()/sleep()/park()/while()
 *  ReentrantLock 实现方式是
 *  while()
 * */

public class ReentrantLockTest {

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                new ReentrantLockTest().drawMoney();
                lock.unlock();
            }
        },"线程1").start();

        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                new ReentrantLockTest().drawMoney();
                lock.unlock();
            }
        },"线程2").start();

    }


    private void drawMoney(){

        System.out.println(Thread.currentThread().getName() + "正在取钱...");

        sleep(3000);

        System.out.println(Thread.currentThread().getName() +"取钱完成...");
    }

    @SneakyThrows
    private void sleep (long millis){
        Thread.sleep(millis);
    }
}
